"""
创建数据库连接
"""
import pymysql


def get_db_conn():
    conn = pymysql.Connection(
        host='localhost',
        port=3306,
        user='root',
        password='123456',
        database='cq',
        autocommit=True  # 设置自动提交
    )
    return conn


def close_db_conn(conn: pymysql.Connection):
    if conn:
        conn.close()


if __name__ == '__main__':
    conn = None
    try:
        conn = get_db_conn()
        cursor = conn.cursor()
        cursor.execute('select * from cq_user')
        print(cursor.fetchall())
    except Exception as e:
        print(e)
    finally:
        close_db_conn(conn)
        print('连接已关闭')
